package com.imodia.checkoo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.AuthConfig;
import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsDocumentBean;
import com.imodia.checkoo.bean.CmsDocumentInfoBean;
import com.imodia.checkoo.bean.CmsModelBean;
import com.imodia.checkoo.bean.CmsOfferJobBean;
import com.imodia.checkoo.bean.CmsUserBean;
import com.imodia.checkoo.service.CmsDataDictionaryService;
import com.imodia.checkoo.service.CmsDocumentInfoService;
import com.imodia.checkoo.service.CmsDocumentService;
import com.imodia.checkoo.service.CmsJobAreaService;
import com.imodia.checkoo.service.CmsModelService;
import com.imodia.checkoo.service.CmsOfferJobService;

@Controller
@RequestMapping("/hfmanager/offer_job/list.do")
public class CmsOfferJobController {
	
	private final Log log = LogFactory.getLog(CmsOfferJobController.class);
	
	private final String baseUrl = "offer_job";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
	private final String url_view = "/hfmanager/" + baseUrl + "/"+baseUrl+"_view_checked";
	
	@Autowired
	private CmsOfferJobService cmsOfferJobService;
	@Autowired
	private CmsDocumentInfoService cmsDocumentInfoService;
	@Autowired
	private CmsModelService cmsModelService;
	@Autowired
	private CmsDocumentService cmsDocumentService;
	@Autowired
	private CmsDataDictionaryService cmsDataDictionaryService;
	@Autowired
	private CmsJobAreaService cmsJobAreaService;
	
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		int documentId = StringUtil.requestInt(request,"documentId");
		
		cmsOfferJobService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&documentId="+documentId), null);
	}
	
	@RequestMapping(params = "action=checked")
	public ModelAndView checked(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		int documentId = StringUtil.requestInt(request,"documentId");
		cmsOfferJobService.checked(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&documentId="+documentId), null);
	}

	
	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsOfferJobBean obj, HttpServletRequest request) {
		int x = 0;
		String  areaNames =  cmsDataDictionaryService.queryAreaByIds(request.getParameterValues("areaIds"));
		obj.setArea(areaNames);
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsOfferJobService.update(obj);
		}else{
			x = cmsOfferJobService.insert(obj);
			obj.setId(x);
		}
		if(x>0){
			x=1;
			cmsJobAreaService.save(request.getParameterValues("areaIds"), obj.getId());
		}
		int documentId = StringUtil.requestInt(request, "documentId");
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&id="+obj.getId()+"&x="+x+"&documentId="+documentId), null);
	}
	
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsOfferJobBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		
		m.put("info", cmsOfferJobService.get(id));
		
		int documentId = StringUtil.requestInt(request, "documentId");
		
		SqlParam sql = new SqlParam();
		sql.andEqualTo("document_id", String.valueOf(documentId));
		sql.andOrderListValue("priority", "");
		List<CmsDocumentInfoBean> documentList = cmsDocumentInfoService.list(sql);
		m.put("documentList", documentList);
		
		CmsDocumentBean document = cmsDocumentService.get(documentId);
		m.put("document", document);
	
		SqlParam sqlModel = new SqlParam();
		sqlModel.andEqualTo("ref_id", String.valueOf(documentId));
		sqlModel.andEqualTo("ref_rs_id", "3");
		List<CmsModelBean> modelList = cmsModelService.list(sqlModel);
		m.put("model", modelList);
		
		List<CmsDataDictionaryBean> areaList = cmsDataDictionaryService.getClildInfo(9);
		if(id>0){
			cmsJobAreaService.setSelected(areaList, id);
		}
		m.put("areaList", areaList);
		
	    return new ModelAndView(url_edit, m);
	}
	
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsOfferJobBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		SqlParam sql = new SqlParam();
		CmsUserBean user = (CmsUserBean)request.getSession().getAttribute(AuthConfig.SESSION_USER_INFO);
		if(user!=null && user.getId()>1){
			if(request.getSession().getAttribute(AuthConfig.SESSION_AREA_LIST)!=null){
				List<CmsDataDictionaryBean>  areaList = (List<CmsDataDictionaryBean>)request.getSession().getAttribute(AuthConfig.SESSION_AREA_LIST);
				List<String> areaString = new ArrayList<String>();
				for(CmsDataDictionaryBean area:areaList){
					areaString.add(String.valueOf(area.getId()));
				}
				if(areaString.size()>0){
					sql.andIn("area", areaString);
				}else{
					sql.andNotEqualTo(" 1 ", "1");
				}
			}
		}

		PageResult<CmsOfferJobBean> pageResult =   cmsOfferJobService.selectPaginationBySql(page,obj);
		model.put("page", pageResult);
		
		int documentId = StringUtil.requestInt(request, "documentId");
		CmsDocumentBean document = cmsDocumentService.get(documentId);
		model.put("document", document);
		
		return new ModelAndView(url_list, model);
	}
	
	
	
	@RequestMapping(params = "action=checkedView")
	public ModelAndView checkedView(HttpServletRequest request,CmsOfferJobBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();

		
		m.put("info", cmsOfferJobService.get(id));
		
		int documentId = StringUtil.requestInt(request, "documentId");
		
		SqlParam sql = new SqlParam();
		sql.andEqualTo("document_id", String.valueOf(documentId));
		sql.andOrderListValue("priority", "");
		List<CmsDocumentInfoBean> documentList = cmsDocumentInfoService.list(sql);
		m.put("documentList", documentList);
		
		CmsDocumentBean document = cmsDocumentService.get(documentId);
		m.put("document", document);
	
		SqlParam sqlModel = new SqlParam();
		sqlModel.andEqualTo("ref_id", String.valueOf(documentId));
		sqlModel.andEqualTo("ref_rs_id", "3");
		List<CmsModelBean> modelList = cmsModelService.list(sqlModel);
		m.put("model", modelList);
		
		
		List<CmsDataDictionaryBean> areaList = cmsDataDictionaryService.getClildInfo(9);
		cmsJobAreaService.setSelected(areaList, id);
		m.put("areaList", areaList);
		
	    return new ModelAndView(url_view, m);
	}
	
}
